// import { login, logout, getInfo } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
// import { resetRouter } from '@/router'
import { login, getUserProfile, getUserDetailById } from '@/api/user'
// import { remove } from 'js-cookie'
const state = {
  token: getToken() || '',
  userInfo: {
  }
}

const mutations = {
  // 设置token
  setToken: (state, token) => {
    state.token = token
    setToken(token)
  },
  removeToken: (state) => {
    state.token = ''
    removeToken(state)
  },
  // 设置用户信息
  setuserInfo(state, info) {
    state.userInfo = info
  },
  // 删除用户信息
  removeInfo(state) {
    state.info = ''
  }
}

const actions = {
  // 登录
  async UserLogin(context, params) {
    const res = await login(params)
    console.log(res)
    context.commit('setToken', res.data)
  },
  // 用户信息
  async GetUserProfile(context) {
    const res = await getUserProfile()
    // console.log(res)
    const res2 = await getUserDetailById(res.data.userId)
    // console.log(res2)
    context.commit('setuserInfo', { ...res.data, ...res2.data })
    console.log(state.userInfo)
  },
  userLogout(context) {
    context.commit('removeToken')
    context.commit('removeInfo')
  }
  // 获取用户头像
  // 获取用户信息
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

